package com.beanbean.mvvmtest.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.ViewGroup;
import android.widget.Toast;

import com.beanbean.mvvmtest.BR;
import com.beanbean.mvvmtest.R;
import com.beanbean.mvvmtest.base.BaseAdapter;
import com.beanbean.mvvmtest.base.BaseViewHolder;
import com.beanbean.mvvmtest.bean.UserInfo;

public class ListAdapter extends BaseAdapter<UserInfo, BaseViewHolder> {
    private final static String TAG = ListAdapter.class.getSimpleName();

    public ListAdapter(Context context) {
        super(context);
    }

    /**
     * 创建 View Holder
     *
     * @param parent   parent
     * @param viewType item type
     * @return view holder
     */
    @Override
    public BaseViewHolder onCreateVH(ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_user, parent, false);
        return new BaseViewHolder(binding);
    }

    /**
     * 绑定 View Holder
     *
     * @param baseViewHolder view holder
     * @param position       position
     */
    @Override
    public void onBindVH(BaseViewHolder baseViewHolder, int position) {
        ViewDataBinding binding = baseViewHolder.getBinding();
        binding.setVariable(BR.adapter, this);
        binding.setVariable(BR.position, position);
        binding.setVariable(BR.user, mList.get(position));
        binding.executePendingBindings(); //防止闪烁
    }

    public void itemClick(UserInfo info, int position) {
        Toast.makeText(mContext, "click " + info.getName(), Toast.LENGTH_LONG).show();
    }
}
